import logging
import json

from base.request import BaseHandler
from conf.config import FACTORY_UID

logger = logging.getLogger("wechat")


class Mall(BaseHandler):
    def data_received(self, chunk):
        pass

    async def get(self):
        open_id = await self.get_open_id()

        if not open_id:
            open_id = "999999999"

        class Goods(object):
            pass

        class Carts(object):
            pass

        session = self.get_db_session([
            {"db_table": "sdet_goods", "model": Goods},
            {"db_table": "sdet_cart", "model": Carts},
        ])
        goods_array = session.query(Goods).filter_by(sdef_goods_status=1, sdef_goods_is_delete=0,
                                                     sdef_goods_factory_uid=FACTORY_UID).order_by(
            Goods.sdef_goods_order_num.asc(), Goods.sdef_goods_price.asc()).all()
        cart_array = session.query(Carts).filter_by(sdef_cart_device_id=open_id).all()

        cart_count = 0
        if len(cart_array) > 0:
            cart_goods = json.loads(cart_array[0].sdef_cart_goods)
            for g in cart_goods:
                cart_count = cart_count + g["count"]

        context = dict(
            text="商城",
            goods_array=goods_array,
            cart_count=cart_count,
            device_id=open_id,
            is_membership=self.is_membership(open_id),
        )

        template = "pad_mall/mall.html"
        return self.render_html(template, **context)


class SwitchHandler(BaseHandler):
    def data_received(self, chunk):
        pass

    def get(self, goods_id):
        context = dict(
            goods_id=goods_id,
            title="处理中",
            description="请求处理中请稍后...",
        )
        return self.render_html("pad_mall/switch.html", **context)


class PayConfirm(BaseHandler):
    def data_received(self, chunk):
        pass

    async def get(self):
        open_id = await self.get_open_id()

        class UserAddress(object):
            pass

        session = self.get_db_session([
            {"db_table": "sdet_address", "model": UserAddress},
        ])

        address_arr = session.query(UserAddress).filter_by(
            sdef_address_device_id=open_id,
            sdef_address_is_del=0).order_by(UserAddress.sdef_address_id.desc()).limit(1).all()

        if len(address_arr) == 1:
            address = address_arr[0]
        else:
            master = self.get_device_master_open_id(open_id)
            if master:
                address_arr = session.query(UserAddress).filter_by(
                    sdef_address_device_id=master,
                    sdef_address_is_del=0).order_by(UserAddress.sdef_address_id.desc()).limit(1).all()
                if len(address_arr) > 0:
                    address = address_arr[0]
                else:
                    address = None
            else:
                address = None

        context = dict(
            text="扫码付款",
            open_id=open_id,
            device_id=open_id,
            address=address,
        )
        return self.render_html("pad_mall/pay-confirm.html", **context)
